﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TRPOP.CRM.Component.Model;
using TRPOP.CRM.Component.DALSQLServer;
using TRPOP.Common;

namespace TRPOP.CRM.Component.Business
{
    /// <summary>
    /// 客户信息管理
    /// </summary>
    public class CompanyManage
    {
        /// <summary>
        /// 获取所有的 CompanyExtraInfo List
        /// </summary>
        /// <param name="intPageNumber"></param>
        /// <param name="intPageSize"></param>
        /// <param name="strCondition"></param>
        /// <param name="strOrderby"></param>
        /// <param name="intRecordCount"></param>
        /// <returns></returns>
        public static List<CompanyExtraInfo> GetExtraInfoListByPager(int intPageNumber, int intPageSize, string strCondition, string strOrderby, out int intRecordCount)
        {
            if (strOrderby.IsEmpty())
            {
                strOrderby = "iCompanyID DESC";
            }

            return new CompanyDAL().GetExtraInfoListByPager(intPageNumber, intPageSize, strCondition, strOrderby, out  intRecordCount);
        }
        /// <summary>
        /// 获取客户信息
        /// </summary>
        /// <returns></returns>
        public static CompanyInfo GetCompanyInfo(int intID)
        {
            return new CompanyDAL().GetInfoById(intID);
        }
        /// <summary>
        /// 删除客户信息
        /// </summary>
        /// <param name="intID"></param>
        /// <returns></returns>
        public static bool DeleteInfoById(int intID)
        {
            try
            {
                return new CompanyDAL().DeleteInfoById(intID);
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 批量 删除客户信息
        /// </summary>
        /// <param name="intID"></param>
        /// <returns></returns>
        public static bool DeleteInfoByIds(string strIDs)
        {
            try
            {
                return new CompanyDAL().DeleteInfoByIds(strIDs);
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 检测 客户编号是否存在
        /// </summary>
        /// <param name="strCompanyNumber"></param>
        /// <returns></returns>
        public static bool CheckCompanyNumberExist(string strCompanyNumber)
        {
            return new CompanyDAL().CheckCompanyNumberExist(strCompanyNumber);
        }

        /// <summary>
        /// 获取所有的 Company + 计划
        /// </summary>
        /// <param name="intPageNumber"></param>
        /// <param name="intPageSize"></param>
        /// <param name="strCondition"></param>
        /// <param name="strOrderby"></param>
        /// <param name="intRecordCount"></param>
        /// <returns></returns>
        public static List<CompanyRemindExtraInfo> GetRemindExtraInfoListByPager(int intPageNumber, int intPageSize, string strCondition, string strOrderby, out int intRecordCount)
        {
            if (strOrderby.IsEmpty())
            {
                strOrderby = "[dtStartTime] , [iCompanyID] DESC";
            }
            return new CompanyDAL().GetRemindExtraInfoListByPager(intPageNumber, intPageSize, strCondition, strOrderby, out  intRecordCount);
        }

        /// <summary>
        /// 获取我的工作计划   计划 + Company 
        /// </summary>
        /// <param name="intPageNumber"></param>
        /// <param name="intPageSize"></param>
        /// <param name="strCondition"></param>
        /// <param name="strOrderby"></param>
        /// <param name="intRecordCount"></param>
        /// <returns></returns>
        public static List<CompanyRemindExtraInfo> GetRemindCompanyListByPager(int intPageNumber, int intPageSize, string strCondition, string strOrderby, out int intRecordCount)
        {
            if (strOrderby.IsEmpty())
            {
                strOrderby = " [bIsEnable] DESC , [dtStartTime] , [iCompanyID] DESC";
            }
            return new CompanyDAL().GetRemindCompanyListByPager(intPageNumber, intPageSize, strCondition, strOrderby, out  intRecordCount);
        }

        /// <summary>
        /// 保存 工作计划
        /// </summary>
        /// <param name="strCompanyIDs"></param>
        /// <param name="oRemindCategoryInfo"></param>
        /// <returns></returns>
        public static bool SaveRemindCompany(string strCompanyIDs, int intRemindCategoryInfoID, int intEmployeeID)
        {
            RemindCategoryInfo oRemindCategoryInfo = BaseManage.GetRemindCategoryInfoById(intRemindCategoryInfoID);

            if (oRemindCategoryInfo == null)
                return false;

            CompanyRemindInfo oCompanyRemindInfo = new CompanyRemindInfo();

            oCompanyRemindInfo.bIsEnable = true;
            oCompanyRemindInfo.dtCreationTime = DateTime.Now;
            oCompanyRemindInfo.dtEndTime = DateTime.Now.AddDays(oRemindCategoryInfo.iIntervalDays).ToString("yyyy-MM-dd 23:59:59").ToDateTime();
            oCompanyRemindInfo.dtStartTime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00").ToDateTime();
            oCompanyRemindInfo.iEmployeeID = intEmployeeID;
            oCompanyRemindInfo.iRemindCategoryID = oRemindCategoryInfo.iRemindCategoryID;

            return new CompanyRemindDAL().SaveRemindCompany(strCompanyIDs, oCompanyRemindInfo);
        }
        /// <summary>
        /// 检测 客户姓名是否存在
        /// </summary>
        /// <param name="strCompanyNumber"></param>
        /// <returns></returns>
        public static bool CheckCompanyNameExist(string strCompanyNumber)
        {
            return new CompanyDAL().CheckCompanyNameExist(strCompanyNumber);
        }
        /// <summary>
        /// 保存客户信息
        /// </summary>
        /// <param name="oCompanyInfo"></param>
        /// <returns></returns>
        public static bool SaveCompanyInfo(CompanyInfo oCompanyInfo)
        {
            return new CompanyDAL().Save(oCompanyInfo);
        }

        /// <summary>
        /// 踢出 失效的工作计划
        /// </summary>
        /// <returns></returns>
        public static bool UpdateRemindCompany()
        {
            return new CompanyRemindDAL().DeleteInfoById(string.Empty);
        }

        /// <summary>
        /// 当跟踪某一个客户后，自动将该客户从工作计划中踢出
        /// </summary>
        /// <param name="intCompanyID"></param>
        /// <param name="intEmployeeID"></param>
        /// <returns></returns>
        public static bool UpdateRemindCompany(int intCompanyID, int intEmployeeID)
        {
            return new CompanyRemindDAL().DeleteInfoById(string.Format(" AND [iEmployeeID]= {0} AND [iCompanyID]={1}", intEmployeeID, intCompanyID));
        }

        /// <summary>
        /// 获取 WorkingLogExtraInfo 分页信息
        /// </summary>
        /// <param name="intPageNumber"></param>
        /// <param name="intPageSize"></param>
        /// <param name="strCondition"></param>
        /// <param name="strOrderby"></param>
        /// <param name="intRecordCount"></param>
        /// <returns></returns>
        public static List<WorkingLogExtraInfo> GetWorkingLogPager(int intPageNumber, int intPageSize, string strCondition, string strOrderby, out int intRecordCount)
        {
            if (strOrderby.IsEmpty())
            {
                strOrderby = " [iWorkingLogID] DESC ";
            }
            return new WorkingLogDAL().GetWorkingLogPager(intPageNumber, intPageSize, strCondition, strOrderby, out  intRecordCount);
        }

        /// <summary>
        /// 批注分页信息
        /// </summary>
        /// <param name="intPageNumber"></param>
        /// <param name="intPageSize"></param>
        /// <param name="strCondition"></param>
        /// <param name="strOrderby"></param>
        /// <param name="intRecordCount"></param>
        /// <returns></returns>
        public static List<WorkCommentExtraInfo> GetExtraListByPager(int intPageNumber, int intPageSize, string strCondition, string strOrderby, out int intRecordCount)
        {
            if (strOrderby.IsEmpty())
            {
                strOrderby = " [dtWorkingLogTime] DESC ";
            }
            return new WorkCommentDAL().GetExtraListByPager(intPageNumber, intPageSize, strCondition, strOrderby, out  intRecordCount);
        }
        
    }
}
